local base_view = require 'views.view'
local color = require 'color'
local division = require 'division'
local vec2 = require 'vec2'
local constrained_size = require 'constrained_size'

---@class text: view
---@field text string
---@field color vec4
---@field font_size number
local text = base_view:new_view_type()

---@class text.renderer: view.renderer
---@field id number entity_id
text.renderer = base_view.renderer:new_renderer_type()

---@param options { text: string, color: vec4, font_size: number }
---@return text
function text:__call(options)
    self:set_view_prototype(options)

    options.color = options.color or color.BLACK
    options.text = options.text or ''
    options.font_size = options.font_size or 16

    return options
end

function text:create_renderer_instance()
    return self:set_renderer_prototype({
        id = division:create_drawable_text()
    })
end

---@param constraints constraints
---@param view text
function text.renderer:layout(constraints, view)
    return constrained_size.unconstrained()
end

---@param render_rect rect
---@param view text
function text.renderer:render(render_rect, view)
    division:set_drawable_text(
        self.id, view.text, render_rect, view.color, view.font_size)
end

function text.renderer:destroy()
    division:destroy_entity(self.id)
end

return text
